#include"Queue.h"

void QueueInit(QU* q)
{
	assert(q);
	q->head = NULL;
	q->tail = NULL;
	q->size = 0;
}
void QueueDestory(QU* q)
{
	assert(q);
	while (q->head != q->tail)
	{
		QNode* cur = q->head;
		q->head = q->head->next;
		free(cur);
	}
	free(q->head);
}

void QueuePush(QU* q, QUdatatype x)
{
	assert(q);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc failed");
		exit(-1);
	}
	newnode->data = x;
	newnode->next = NULL;
	if (q->head == NULL)
	{
		q->head = newnode;
		q->tail = newnode;
	}
	else
	{
		q->tail->next = newnode;
		q->tail = newnode;
	}
	q->size++;
}
void QueuePop(QU* q)
{
	assert(q);
	assert(q->head != NULL);
	if (q->head == q->tail)
	{
		free(q->head);
		q->head = NULL;
		q->tail = NULL;
	}
	else
	{
		QNode* next = q->head->next;
		free(q->head);
		q->head = next;
	}
	q->size--;
}

QUdatatype QueueFront(QU* q)
{
	assert(q);
	assert(q->head);
	if (!QueueEmpty(q));
	return q->head->data;
}
int QueueSize(QU* q)
{
	assert(q);
	assert(q->size);
	if (!QueueEmpty(q))
		return q->size;
	return 0;
}
bool QueueEmpty(QU* q)
{
	assert(q);

	return q->head == NULL;
}

//void QueuePrint(QU* q)
//{
//	assert(q);
//	while (!QueueEmpty(q))
//	{
//		printf("%d   ", QueueFront(&q));
//		QueuePop(q);
//	}
//	printf("\n");
//}